package com.atguigu.web;

import cn.hutool.core.util.IdUtil;
import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.UploadObjectArgs;
import io.minio.errors.MinioException;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

@DisplayName("文件上传测试")
public class FileUploader {
    @DisplayName("文件上传")
    @Test
    public void fileUpload() throws Exception {
        try {
            // 使用 MinIO 服务器 Playground、访问地址，其访问密钥和私有密钥创建一个 minioClient。
            MinioClient minioClient =
                    MinioClient.builder()
                            .endpoint("http://192.168.100.188:9000/")
                            .credentials("SUMOfWifqfa4AHjeM8he", "unUX4lfNTEFVkAAVfIe6tay2ZrMDyeiSEus6XLCT")
                            .build();

            //判断桶是否存在 如果不存在，请创建 'asiatrip' 存储桶
            boolean found =
                    minioClient.bucketExists(BucketExistsArgs.builder().bucket("java-136-minio").build());
            if (!found) {
                // Make a new bucket called 'asiatrip'.
                minioClient.makeBucket(MakeBucketArgs.builder().bucket("java-136-minio").build());
            } else {
                System.out.println("Bucket 'java-136-minio' already exists.");
            }
            String fileName = IdUtil.fastSimpleUUID() + ".png";
            // 将 homeuserPhotosasiaphotos.zip 作为对象名称 asiaphotos-2015.zip 上传到存储桶
            // 'asiatrip'.
            minioClient.uploadObject(
                    UploadObjectArgs.builder()
                            .bucket("java-136-minio")
                            .object(fileName)
                            .filename("C:\\Users\\luoran\\Desktop\\毕设所有内容\\爱宠\\login-l.png")
                            .build());
            System.out.println("上传成功");
        } catch (MinioException e) {
            System.out.println("Error occurred: " + e);
            System.out.println("HTTP trace: " + e.httpTrace());
        }
    }
}